package com.chen.qmzy.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.chen.qmzy.controller.form.AddressInsertForm;
import com.chen.qmzy.controller.form.AddressUpdateForm;
import com.chen.qmzy.controller.vo.AddressVO;
import com.chen.qmzy.entity.DwReceiverEntity;
import com.chen.qmzy.entity.DwUserEntity;
import com.chen.qmzy.entity.ResultEntity;
import com.chen.qmzy.exception.BusinessException;
import com.chen.qmzy.exception.BusinessExceptionEnum;
import com.chen.qmzy.service.DwReceiverService;
import com.chen.qmzy.service.util.BeanUtils;
import com.chen.qmzy.tool.Result;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@CrossOrigin//跨域
@RestController//遵循RestFul
@RequestMapping("v2/DwRe/")
public class DwReController {
    @Resource
    private RedisTemplate<String ,String> redisTemplate;//Redis工具类

    @Resource
    DwReceiverService dwReceiverService;

    /**
     * 添加地址1
     */
    @SaCheckLogin
    @PostMapping("/insert")
    public Result insert(@RequestBody AddressInsertForm form) {
        Integer userId = StpUtil.getLoginIdAsInt();
        //数据准备
        DwReceiverEntity address = new DwReceiverEntity();
        BeanUtils.copyProperties(form, address);
        address.setUserid(userId);
        //增加
        if (dwReceiverService.addReceiver(
                address.getUserid(), address.getReceiver(), address.getCellphone(), address.getProvince(),
                address.getCity(), address.getCounty(), address.getAddress(), address.getAreacode(), address.getIsdefault()
        )) {
            return Result.ok();
        }
        return Result.error();
    }
    /**
     * 删除
     * @param id 根据id删除
     * @return json
     */
    @SaCheckLogin
    @DeleteMapping("/delReceiver")
    public Result delReceiver(@RequestParam(value = "id",defaultValue = "-1") int id){
        Integer userId = StpUtil.getLoginIdAsInt();
        //检查地址
        DwReceiverEntity addressDB = dwReceiverService.getReceiverDetailList(id);
        if (!userId.equals(addressDB.getUserid())) {
            throw new BusinessException(BusinessExceptionEnum.REQUEST_FORBIDEN_DATA_NOT_YOUR);
        }
        //删除
        if (dwReceiverService.delReceiver(id)) {
            return Result.ok();
        }
        return Result.error();

    }
    /**
     * 根据id修改用户信息
     */
    @SaCheckLogin
    @PostMapping("upReceiver")
    public Result upReceiver(
            @RequestBody AddressUpdateForm form
            ){
        Integer userId = StpUtil.getLoginIdAsInt();
        DwReceiverEntity address = dwReceiverService.getReceiverDetailList(form.getId());
        if (!userId.equals(address.getUserid())) {
            throw new BusinessException(BusinessExceptionEnum.REQUEST_FORBIDEN_DATA_NOT_YOUR);
        }
        //数据准备
        BeanUtils.copyProperties(form, address);
        //更改
        if (dwReceiverService.upReceiver(address.getUserid(),
                address.getReceiver(), address.getCellphone(), address.getCity(),
                address.getCounty(), address.getAddress(), address.getAreacode(),
                address.getIsdefault(), address.getId())) {
            System.out.println(address.getIsdefault());
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 根据userId查询 登录后的用户列表信息 查询我的收货地址列表
     * @return json
     */
    @SaCheckLogin
    @GetMapping("getReceiverList")
    public String getReceiverList(){
        Integer userId = StpUtil.getLoginIdAsInt();
        return dwReceiverService.getReceiverList(userId);
    }


    /**
     * 根据id查询用户列表详情信息
     * @param id id
     * @return json
     */
    @SaCheckLogin
    @GetMapping("getReceiverDetailList")
    public Result<AddressVO> getReceiverDetailList(@RequestParam(value = "id",defaultValue = "-1") int id){
        Integer userId = StpUtil.getLoginIdAsInt();
        //查询数据，并检查
        DwReceiverEntity address = dwReceiverService.getReceiverDetailList(id);
        if (!userId.equals(address.getUserid())) {
            throw new BusinessException(BusinessExceptionEnum.REQUEST_FORBIDEN_DATA_NOT_YOUR);
        }
        //数据格式转换，并返回
        AddressVO addressVO = new AddressVO();
        BeanUtils.copyProperties(address, addressVO);
        return Result.ok(addressVO);
    }

}
